//package com.haiyou.common.aspect;
//
//import com.alibaba.fastjson.JSON;
//import com.haiyou.common.anno.RedisCache;
//import com.haiyou.common.util.R;
//import com.haiyou.common.util.RedisUtil;
//import com.haiyou.common.util.RequestUtils;
//import com.haiyou.common.util.StringUtils;
//import lombok.extern.slf4j.Slf4j;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.Signature;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Pointcut;
//import org.aspectj.lang.reflect.MethodSignature;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.core.annotation.Order;
//import org.springframework.stereotype.Component;
//import org.springframework.web.servlet.HandlerMapping;
//
//import javax.servlet.http.HttpServletRequest;
//import java.lang.annotation.Annotation;
//import java.lang.reflect.Method;
//import java.util.Map;
//
///**
// * redis缓存切面
// */
//@Aspect
//@Component
//@Slf4j
//@Order(100)
//public class RedisCacheAspect {
//
//    @Autowired
//    private RedisUtil redisUtil;
//
//    @Pointcut("@annotation(com.haiyou.common.anno.RedisCache)")
//    public void pointCut() {
//
//    }
//
//    @Around("pointCut()")
//    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
//        log.info("RedisCacheAspect代理类开始............");
//
//        RedisCache redisCache = null;
//        HttpServletRequest request = RequestUtils.getRequest();
//        Map<String, String> pathVars = (Map<String, String>) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
//
//        StringBuilder reqParam = new StringBuilder();
//        Signature signature = joinPoint.getSignature();
//        MethodSignature methodSignature = (MethodSignature) signature;
//        Method targetMethod = methodSignature.getMethod();
//        Annotation[] annotations = targetMethod.getAnnotations();
//        for (Annotation annotation : annotations) {
//            if (annotation.annotationType().equals(RedisCache.class)) {
//                redisCache = (RedisCache) annotation;
//                log.debug("redisCache.key()={}", redisCache.key());
//                String[] splitKey = redisCache.key().split(redisCache.delimiter());
//                for (String key : splitKey) {
//                    if (reqParam.length() > 0) {
//                        reqParam.append(redisCache.delimiter());
//                    }
//                    log.debug("key={},value={}", key, pathVars.get(key));
//                    reqParam.append(pathVars.get(key));
//                }
//                break;
//            }
//        }
//
//        if (redisCache == null) {
//            return joinPoint.proceed();
//        }
//
//        reqParam.insert(0, redisCache.prefix() + ":");
//        String redisKey = reqParam.toString();
//        String result = redisUtil.get(redisKey);
//        log.info("key={}", redisKey);
//        if (StringUtils.isNotEmpty(result)) {
//            log.debug("**********从Redis中查到了数据**********");
//            log.debug("Redis的key={}，value={}", redisKey, result);
//            R r = JSON.parseObject(result, R.class);
//            return r;
//        }
//        log.info("**********没有从Redis查到数据**********");
//        try {
//            Object obj = joinPoint.proceed();
//            redisUtil.set(redisKey, obj, redisCache.expire());
//            return obj;
//        } catch (Throwable e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
//
//
//}
